<?php

namespace App\Http\Controllers;

use App\Models\KuaibaoDay;
use App\Models\KuaibaoMonth;
use App\Models\KuaibaoMonthBrand;
use App\Models\Resident;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Validator;

use App;
use App\Contracts\TestContract;
class TestController extends Controller
{
    //测试队列
    public function testQueue()
    {
//        event(new App\Events\OrderShipped());
        //job任务
        App\Jobs\ProcessPodcast::dispatch()
//            ->onConnection('redis') // 指定连接
            ->onQueue('processing'); // 指定队列;

        //事件系统
//        App\Events\OrderShipped::dispatch();
        return 'success';
    }


    //测试服务提供者
    protected $test;
    //依赖注入
    public function __construct(TestContract $test){
        $this->test = $test;
    }

    public function test1(){
        //$test = App::make('test');
        //$test->callMe('TestController1');

        $this->test->callMe('TestController');
    }


    //测试控制器
    public function index(Request $request)
    {
        $param = $request->input();

//        $day = date('Y-m-d',strtotime('-1 day'));
//        $res_day = KuaibaoDay::where('day',$day)
//            ->select('id','recived','sended','stock')
//            ->first()->toArray();
//        //上月数据统计
//        $month = date('Y-m',strtotime('last month'));
//        $res_month = KuaibaoMonth::where('month',$month)
//            ->select('id','recived_money','sended_money','paotui_money')
//            ->first()->toArray();
//        $res = [
//            'recived' => $res_day['recived']??0,
//            'sended' => $res_day['sended']??0,
//            'stock' => $res_day['stock']??0,
//            'recived_money' => 0.46,
//            'sended_money' => 0,
//            'paotui_money' => 0,
//        ];
//        if($res_month){
//            $recived_money = $res_month['recived_money']/10000;
//            if($recived_money > 0.01){//大于等于0.01万
//                $res['recived_money'] = sprintf("%.2f",$recived_money);
//            }
//        }
//        //上月快递分布
//        $res_band = KuaibaoMonthBrand::where('month',$month)
//            ->select('id','brand_name','recived','total')
//            ->orderBy('recived','desc')
//            ->limit(4)
//            ->get()->toArray();
//        $band_list = [];
//        $band_rate = 0;//其他快递百分比
//        foreach($res_band as $k=>$value){
//            $band_rate += 0;
//            $band_list[$k]['name'] = str_replace('快递','',$value['brand_name']);
//            $rate = sprintf("%.2f",$value['recived']/$value['total'])*100;
//            $band_list[$k]['rate'] = $rate;
//            $band_rate += $rate;
//            //其他快递
//            if($value == end($res_band)) {
//                $band_list[$k+1]['name'] = '其他';
//                $band_list[$k+1]['rate'] = 100 - $band_rate;
//            }
//        }
//        $res['band_list'] = $band_list;
        //获取所有数据
        //$res['resident'] = Resident::limit(10)->get()->toArray();

        //获取指定数据
//        $res = Resident::whereIn('user_id',[388,299])->pluck('real_name');

        //分页数据
        $where = [
            ['user_id','=',388]
        ];
//        $res = Resident::where($where)->paginate(1);

        //关联查询
//        $res = Resident::whereIn('user_id',[388,299,676])->get();
//        foreach($res as $value){
//            var_dump($value->sign);//不断查询
//        }

        //关联预载入查询
        $res = Resident::with(['sign'=>function ($query) {
//            $query->where('order_num', 'like', 'jh%');
        }])->whereIn('user_id',[388,299])->paginate(1);
        foreach($res as $book){
            var_dump($book->sign);
        }
        return $res;
    }

    public function form()
    {
        return view('form');
    }

    public function receive(Request $request)
    {
//        $request->validate([
//            'username'      =>  'required|min:2|max:10',
//            'password'      =>  'required|min:6'
//        ]);

        //dd($request->validated());


        $validator = Validator::make($request->post(), [
            //'username'      =>  'required|min:2|max:10',
            //'username'      =>  'required|alpha_dash',
            //'username'      =>  'required|between:2,6',
            //'username'      =>  'required|size:2',
            //'username'      =>  'required|email',
            //'username'      =>  'required|unique:users',
            //'username'      =>  'required|different:password',
            //'username'      =>  'required|ip',
            //'username'      =>  'required|json',
            //'username'      =>  'required|in:tom,jack,lusy',
//            'username'      =>  [
//                        'required',
//                        Rule::in(['tom', 'jack', 'lusy'])
//            ],

            'username'      =>  Rule::unique('users')->where(function ($query) {
                $query->where('id', 20);
            }),

            'password'      =>  'required|min:6|confirmed'
        ]);

        $validator->after(function ($validator) {
            //判断你要判断的内容
            //$validator->errors()->add('info', '字段info有误~');
        });


        if ($validator->fails()) {
            return redirect('/task/form')->withErrors($validator)->withInput();
        }


        return '恭喜验证成功！';
    }

}
